home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / www / library / implemen / htlist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  2.8 KB  |  132 lines

  1. /*    A small List class                          HTList.c
  2. **    ==================
  3. **
  4. **    A list is represented as a sequence of linked nodes of type HTList.
  5. **    The first node is a header which contains no object.
  6. **    New nodes are inserted between the header and the rest of the list.
  7. */
  8.  
  9. #include"capalloc.h"
  10. #include "HTList.h"
  11. #include"capstdio.h"
  12.  
  13. #include <stdio.h>                /* joe@athena, TBL 921019 */
  14.  
  15. HTList * HTList_new NOARGS
  16. {
  17.   HTList *newList = (HTList *)malloc (sizeof (HTList));
  18.   if (newList == NULL) outofmem(__FILE__, "HTList_new");
  19.   newList->object = NULL;
  20.   newList->next = NULL;
  21.   return newList;
  22. }
  23.  
  24. void HTList_delete ARGS1(HTList *,me)
  25. {
  26.   HTList *current;
  27.   while (current = me) {
  28.     me = me->next;
  29.     free (current);
  30.   }
  31. }
  32.  
  33. void HTList_addObject ARGS2(HTList *,me, void *,newObject)
  34. {
  35.   if (me) {
  36.     HTList *newNode = (HTList *)malloc (sizeof (HTList));
  37.     if (newNode == NULL) outofmem(__FILE__, "HTList_addObject");
  38.     newNode->object = newObject;
  39.     newNode->next = me->next;
  40.     me->next = newNode;
  41.   }
  42.   else    {
  43. #ifndef RELEASE
  44.     if (TRACE) fprintf(stderr,
  45.     "HTList: Trying to add object %p to a nonexisting list\n",
  46.                newObject);
  47. #endif /* RELEASE */
  48.   }
  49. }
  50.  
  51. BOOL HTList_removeObject ARGS2(HTList *,me, void *,oldObject)
  52. {
  53.   if (me) {
  54.     HTList *previous;
  55.     while (me->next) {
  56.       previous = me;
  57.       me = me->next;
  58.       if (me->object == oldObject) {
  59.     previous->next = me->next;
  60.     free (me);
  61.     return YES;  /* Success */
  62.       }
  63.     }
  64.   }
  65.   return NO;  /* object not found or NULL list */
  66. }
  67.  
  68. void * HTList_removeLastObject ARGS1 (HTList *,me)
  69. {
  70.   if (me && me->next) {
  71.     HTList *lastNode = me->next;
  72.     void * lastObject = lastNode->object;
  73.     me->next = lastNode->next;
  74.     free (lastNode);
  75.     return lastObject;
  76.   } else  /* Empty list */
  77.     return NULL;
  78. }
  79.  
  80. void * HTList_removeFirstObject ARGS1 (HTList *,me)
  81. {
  82.   if (me && me->next) {
  83.     HTList * prevNode;
  84.     void *firstObject;
  85.     while (me->next) {
  86.       prevNode = me;
  87.       me = me->next;
  88.     }
  89.     firstObject = me->object;
  90.     prevNode->next = NULL;
  91.     free (me);
  92.     return firstObject;
  93.   } else  /* Empty list */
  94.     return NULL;
  95. }
  96.  
  97. int HTList_count ARGS1 (HTList *,me)
  98. {
  99.   int count = 0;
  100.   if (me)
  101.     while (me = me->next)
  102.       count++;
  103.   return count;
  104. }
  105.  
  106. int HTList_indexOf ARGS2(HTList *,me, void *,object)
  107. {
  108.   if (me) {
  109.     int position = 0;
  110.     while (me = me->next) {
  111.       if (me->object == object)
  112.     return position;
  113.       position++;
  114.     }
  115.   }
  116.   return -1;  /* Object not in the list */
  117. }
  118.  
  119. void * HTList_objectAt ARGS2 (HTList *,me, int,position)
  120. {
  121.   if (position < 0)
  122.     return NULL;
  123.   if (me) {
  124.     while (me = me->next) {
  125.       if (position == 0)
  126.     return me->object;
  127.       position--;
  128.     }
  129.   }
  130.   return NULL;  /* Reached the end of the list */
  131. }
  132.